//
//  TimeViewController.m
//  Food
//
//  Created by dlios on 15/7/17.
//  Copyright (c) 2015年 李志远. All rights reserved.
//

#import "TimeViewController.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKExtension/SSEShareHelper.h>
#import <ShareSDKUI/ShareSDKUI.h>
#import <ShareSDKUI/SSUIShareActionSheetStyle.h>
@interface TimeViewController ()<MBProgressHUDDelegate>
@property (nonatomic, retain) UIWebView *webView;
@property (nonatomic, retain) NSString *url;
@property (nonatomic, retain) NSString *htmlStr;
@property (nonatomic, retain) MBProgressHUD *progress;
@end

@implementation TimeViewController

- (void)dealloc
{
    [super dealloc];
    [_webView release];
    [_url release];
    [_htmlStr release];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"iconfont-share"] imageWithRenderingMode: UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(share:)];
    [self getData];

}


- (void)share:(UIBarButtonItem *)button
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (r.currentReachabilityStatus == NotReachable) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];

    }else{
    NSLog(@"分享");
#warning 显示内容编辑界面
    //创建分享参数
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:[NSString stringWithFormat:@"%@ http://www.xiangha.com/zhishi/%@.html",self.decripe,self.ID ]

                                     images:@[[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.img]]]]
                                        url:nil
                                      title:@"美食坊"
                                       type:SSDKContentTypeImage];



    [ShareSDK showShareEditor:SSDKPlatformTypeSinaWeibo
           otherPlatformTypes:@[@(SSDKPlatformTypeTencentWeibo), @(SSDKPlatformTypeWechat)]
                  shareParams:shareParams
          onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {

              switch (state) {

                  case SSDKResponseStateBegin:
                  {
                      break;
                  }
                  case SSDKResponseStateSuccess:
                  {
                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                          message:nil
                                                                         delegate:nil
                                                                cancelButtonTitle:@"确定"
                                                                otherButtonTitles:nil];
                      [alertView show];
                      break;
                  }
                  case SSDKResponseStateFail:
                  {
                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                          message:[NSString stringWithFormat:@"%@", error]
                                                                         delegate:nil
                                                                cancelButtonTitle:@"确定"
                                                                otherButtonTitles:nil];
                      [alertView show];
                      break;
                  }
                  case SSDKResponseStateCancel:
                  {
                      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享已取消"
                                                                          message:nil
                                                                         delegate:nil
                                                                cancelButtonTitle:@"确定"
                                                                otherButtonTitles:nil];
                      [alertView show];
                      break;
                  }
                  default:
                      break;
              }

              if (state != SSDKResponseStateBegin)
              {
              }

          }];
    }

}



- (void)createProgress
{
    self.progress = [[MBProgressHUD alloc] initWithView:self.view];
    [self.progress show:YES];
    self.progress.labelText = @"稍等";
    self.progress.detailsLabelText = @"正在加载...";
    [self.view addSubview:self.progress];

}

- (void)hudWasHidden:(MBProgressHUD *)hud
{
    [_progress removeFromSuperview];
    [_progress release];
}

- (void)getData
{
    Reachability *r = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if (r.currentReachabilityStatus == NotReachable) {
        UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"网络连接失败" delegate:self cancelButtonTitle:@"好" otherButtonTitles:nil];
        [alert1 show];
        [alert1 release];
    }else{
        NSString *str = @"http://api.xiangha.com/zhishi5/nousInfo?code=";
        self.url = [NSString stringWithFormat:@"%@%@",str,self.ID];

        [AFNet connectNetGetDataWithURLString:self.url parameters:nil finishblock:^(id result) {
            [self.progress hide:YES];
            self.htmlStr = [[result objectForKey:@"data"] objectForKey:@"html"];
            [self createView];
        }];
        [self createProgress];
    }
}

- (void)createView
{
    self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    self.webView.backgroundColor = [UIColor whiteColor];
    [self.webView loadHTMLString:self.htmlStr baseURL:[NSURL URLWithString:self.url]];
    [self.view addSubview:self.webView];
    [_webView release];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation

 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
